<?php

namespace application\Module;

use application\Comm\Func;
use application\Model\ZxarticleTagRelatModel;
use library\Core\Db\CDbCriteria;
use library\Core\Singleton;
use application\Model\ZxarticleModel;
use application\Comm\Request;

class Zxarticle extends Singleton
{

    /**
     * 列表数据
     * @param array $condition
     * @param int $offset
     * @param int $limit
     * @param int $p_id
     * @return array
     */
    public function getList($select = [], $limit = 10,$p_id = 1)
    {
        $criteria = new CDbCriteria();
        //分页
        $page['now_page'] = Request::getParam('p') ? Request::getParam('p') : 1;
        $page['list_rows'] = Request::getParam('list_rows') ? Request::getParam('list_rows') : $limit;
        $offset = ($page['now_page'] - 1) * $page['list_rows'];
        //排序
        
        $criteria->select($select);
        $criteria->addEqCondition('category_pid', $p_id);
        $criteria->addEqCondition('status', 1);
        $criteria->addEqCondition('is_del', 0);
        $criteria->addOrderDesc('id');
        $criteria->addOrderDesc('orderby');
        $model = new ZxarticleModel();
        $page['total_rows'] = $model->getCount($criteria);
        $criteria->setOffset($offset);
        $criteria->setLimit($limit);

        $data = $model->getListForArray($criteria);
        return ['data' => $data, 'page' => $page];
    }


    /**
     * @param $article_id
     * @return string
     * 获取文章标签列表
     */
    public function zxArticleTagList($article_id)
    {
        $criteria = new CDbCriteria();
        $criteria->select(['zxarticle_tag_id', 'zxarticle_id']);
        $criteria->addLeftJoin(\application\Table\Zxarticletag::getInstance(), 'zx_article_tag.id=zx_article_tag_relat.zxarticle_tag_id', ['name']);
        $criteria->addEqCondition('zx_article_tag_relat.zxarticle_id', $article_id);
        $criteria->addEqCondition('zx_article_tag_relat.is_del', 0);
        $zxadimgModel = new ZxarticleTagRelatModel();
        $data = $zxadimgModel->getListForArray($criteria);
        $result = [];
        if (!empty($data)) {
            foreach ($data as $k => $v) {
                $result[$k]['id'] = $v['zxarticle_tag_id'];
                $result[$k]['aid'] = $v['zxarticle_id'];
                $result[$k]['name'] = $v['name'];
            }
        }

        return $result;
    }

    /**
     * @param $tag_id
     * @param $article_id
     * @return array
     * 检查文章是否已经关联该标签
     */
    public function zxArticleTagRelatCheck($tag_id, $article_id)
    {
        $criteria = new CDbCriteria();
        $criteria->select(['id']);
        $criteria->addEqCondition('zxarticle_id', $article_id);
        $criteria->addEqCondition('zxarticle_tag_id', $tag_id);
        $criteria->addEqCondition('is_del', 0);
        $model = new ZxarticleTagRelatModel();
        return $model->getOne($criteria);
    }

    /**
     * @param $id
     * @return array
     * 获取文章信息
     */
    public function getInfo($id)
    {
        $criteria = new CDbCriteria();
        $select = [
            'id',
            'name',
            'description',
            'content',
            'category_id',
            'category_pid',
            'status',
            'create_time',
        ];
        $criteria->select($select);
        $criteria->addEqCondition('id', $id);
        $zxarticleModel = new ZxarticleModel();
        return $zxarticleModel->getRow($criteria);
    }

    /**
     * @param array $select
     * @param int $limit
     * @return array
     * 获取热门文章
     */
    public function getHot($limit = 10,$p_id = 1,$order)
    {
        $criteria = new CDbCriteria();
        $criteria->select(['id', 'name', 'url']);
        $criteria->addEqCondition('status', 1);
        $criteria->addEqCondition('is_del', 0);
        $criteria->addOrderBy('click_num',$order);
        $criteria->addEqCondition('category_pid', $p_id);
        $model = new ZxarticleModel();
        $criteria->setLimit($limit);
        return $model->getListForArray($criteria);
    }

    /**
     * @param $id
     * @return array
     * 获取上一篇
     */
    public function getPrev($id)
    {
        $criteria = new CDbCriteria();
        $criteria->select(['id', 'name', 'url']);
        $criteria->addLtCondition('id', $id);
        $criteria->addEqCondition('status', 1);
        $criteria->addEqCondition('is_del', 0);
        $criteria->addOrderDesc('id');
        $model = new ZxarticleModel();
        $criteria->setLimit(1);
        return $model->getRow($criteria);
    }

    /**
     * @param $id
     * @return array
     * 获取下一篇
     */
    public function getNext($id)
    {
        $criteria = new CDbCriteria();
        $criteria->select(['id', 'name', 'url']);
        $criteria->addGtCondition('id', $id);
        $criteria->addEqCondition('status', 1);
        $criteria->addEqCondition('is_del', 0);
        $model = new ZxarticleModel();
        $criteria->setLimit(1);
        return $model->getRow($criteria);
    }

    /**
     * @param array $select
     * @param int $limit
     * @return array
     * 获取相关推荐
     */
    public function getRelative($cid, $limit = 10)
    {
        $criteria = new CDbCriteria();
        if ($cid) {
            if ($cid > 9) {
                $criteria->addEqCondition('category_id', $cid);
            } else {
                $criteria->addEqCondition('category_pid', $cid);
            }
        }
        $criteria->select(['id', 'name', 'url']);
        $criteria->addEqCondition('status', 1);
        $criteria->addEqCondition('is_del', 0);
        $model = new ZxarticleModel();
        $count = $model->getCount($criteria);
        $offset = rand(0, $count - $limit - 1);
        $criteria->setOffset($offset);
        $criteria->setLimit($limit);
        $data = $model->getListForArray($criteria);
        $result = [];
        if (!empty($data)) {
            foreach ($data as $k => $v) {
                $result[$k]['id'] = $v['id'];
                $result[$k]['name'] = $v['name'];
                $result[$k]['url'] = $v['url'] ? $v['url'] : Func::createUrl('decorate/view', ['id' => $v['id']]);
            }
        }
        return $result;
    }

    /**
     * @param array $select
     * @param int $limit
     * @return array
     * 获取标签关联文章列表
     */
    public function getTagList($select = [''], $limit = 8)
    {
        $criteria = new CDbCriteria();
        $tid = Request::get('tid');
        //分页
        $page['now_page'] = Request::getParam('p') ? Request::getParam('p') : 1;
        $page['list_rows'] = Request::getParam('list_rows') ? Request::getParam('list_rows') : $limit;
        $offset = ($page['now_page'] - 1) * $page['list_rows'];

        $criteria->select($select);
        if ($tid) $criteria->addEqCondition('zxarticle_tag_id', $tid);
        $criteria->addEqCondition('is_del', 0);
        $criteria->addRightJoin(\application\Table\Zxarticle::getInstance(),'zx_article_tag_relat.zxarticle_id = zx_article.id', ['id','name','description','category_id','like_num']);
        $model = new ZxarticleTagRelatModel();
        $page['total_rows'] = $model->getCount($criteria);
        $criteria->setOffset($offset);
        $criteria->setLimit($limit);

        $data = $model->getListForArray($criteria);
        return ['data' => $data, 'page' => $page];
    }


    /**
     * @param $ad_id
     * @return int
     * 喜欢
     */
    public function Like($id)
    {
        $criteria = new CDbCriteria();
        $criteria->select(['like_num']);
        $criteria->addEqCondition('id', $id);
        $criteria->addEqCondition('status', 1);
        $criteria->addEqCondition('is_del', 0);
        $model = new ZxarticleModel();
        $like = $model->getOne($criteria);
        $like++;

        $criteria_update = new CDbCriteria();
        $criteria_update->update(['like_num'=>$like]);
        $criteria_update->addEqCondition('id', $id);
        return $model->modify($criteria_update);
    }


}
